﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialPortT
{
    public class DetaPLCControl
    {
        private SerialPort _serialPort;

        public DetaPLCControl(SerialPort serialPort)
        {
            this._serialPort = serialPort;
            if (!serialPort.IsOpen)
            {
                this._serialPort.Open();
            }
        }

        public void ClearAllOut()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010F050000060100E4\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY0ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050500FF00F6\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY0OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505000000F5\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY1ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050501FF00F5\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY1OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505010000F4\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY2ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050502FF00F4\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY2OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505020000F3\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY3ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050503FF00F3\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY3OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505030000F2\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public string GetChecks(string str)
        {
            string flag = "";
            if (GetX0(str))
            {
                flag += ",0";
            }
            if (GetX1(str))
            {
                flag += ",1";
            }
            if (GetX2(str))
            {
                flag += ",2";
            }
            if (GetX3(str))
            {
                flag += ",3";
            }
            if (GetX4(str))
            {
                flag += ",4";
            }
            if (GetX5(str))
            {
                flag += ",5";
            }
            if (GetX6(str))
            {
                flag += ",6";
            }
            if (GetX7(str))
            {
                flag += ",7";
            }
            return flag.Trim(',');
        }

        public void SetY4ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050504FF00F2\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY4OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505040000F1\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY5ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050505FF00F1\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY5OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505050000F0\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY6ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050506FF00F0\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY6OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505060000EF\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY7ON()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":01050507FF00EF\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void SetY7OFF()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010505070000EE\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        public void GetAllInCheck()
        {
            if (this._serialPort.IsOpen)
            {
                byte[] data = DetaPLCControl.HexStrToHexByte(":010204000008F1\r\n");
                this._serialPort.Write(data, 0, data.Length);
            }
        }

        private string getStr(string str)
        {
            return str.Substring(3);
        }

        public bool GetX0(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(7, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX1(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(6, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX2(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(5, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX3(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(4, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX4(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(3, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX5(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(2, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX6(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(1, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        public bool GetX7(string str)
        {
            str = this.getStr(str);
            bool result;
            if ("0201".Equals(str.Substring(0, 4)))
            {
                try
                {
                    int tem = Convert.ToInt32(str.Substring(4, 2), 16);
                    string strTem = Convert.ToString(tem, 2).PadLeft(8, '0');
                    if ("1".Equals(strTem.Substring(0, 1)))
                    {
                        result = true;
                        return result;
                    }
                    result = false;
                    return result;
                }
                catch
                {
                    result = false;
                    return result;
                }
            }
            result = false;
            return result;
        }

        private static byte[] HexStrToHexByte(string hexStr)
        {
            byte[] returnByte = Encoding.Unicode.GetBytes(hexStr);
            return Encoding.ASCII.GetBytes(hexStr);
        }
    }
}
